package com.github.ylt.protocol.http.server.request;

import com.github.ylt.protocol.http.Headers;
import com.github.ylt.protocol.http.ImmutableHeaders;
import com.github.ylt.utils.StringUtils;

import java.net.URI;
import java.net.URL;
import java.util.List;

public interface Request {

    URI getRequestURL();
    String getMethod();
    Headers getHeaders();
    String getFullRequestURL();
    default Request withHeaders(String key, List<String> values) {
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("http headers的key不能为空！！");
        }
        if (values == null || values.isEmpty()) {
            throw new IllegalArgumentException("http headers的values不能为空！！");
        }
        final Request  request= this;
        Headers headers = new Headers();
        headers.putAll(request.getHeaders());
        if(!headers.containsKey(key)){
            headers.put(key, values);
        }
        ImmutableHeaders immutableHeaders = new ImmutableHeaders(headers);
        return new Request() {

            @Override
            public URI getRequestURL() {
                return request.getRequestURL();
            }

            @Override
            public String getMethod() {
                return request.getMethod();
            }

            @Override
            public Headers getHeaders() {
                return immutableHeaders;
            }

            @Override
            public String getFullRequestURL() {
                return request.getFullRequestURL();
            }
        };
    }
}
